/**
 * 项目名称:
 *
 * @Author: 夏国倩
 * @Date: 2023/10/12 16:20
 * @Copyright
 */
package multithreading;

/*
类锁，类只有一个，所以锁是类级别的，只有一个
 */
public class ThreadTest17 {
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(new Processor17());
        Thread t2 = new Thread(new Processor17());
        t1.setName("t1");
        t2.setName("t2");

        t1.start();

        //延迟 保证t1先执行
        Thread.sleep(1000);
        t2.start();
    }
}

class Processor17 implements Runnable {

    @Override
    public void run() {
        if ("t1".equals(Thread.currentThread().getName())) {
            MyClass3.m1();
        }
        if ("t2".equals(Thread.currentThread().getName())) {
            MyClass3.m2();
        }
    }
}

class MyClass3 {
    //synchronized添加到静态方法上，线程执行此方法的时候会找类锁
    public synchronized static void m1() {
        try {
            Thread.sleep(10000);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println("m1....");
    }

    public synchronized static void m2() {
        System.out.println("m2....");
    }
}